package com.gzz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzz.common.R;
import com.gzz.common.StringIntersection;
import com.gzz.pojo.Activity;
import com.gzz.pojo.Food;
import com.gzz.service.FoodService;
import com.gzz.service.ImgsRespService;
import com.gzz.service.OssUploadService;
import com.gzz.vo.FoodList;
import com.gzz.vo.FoodVo;
import com.gzz.vo.SenceryDetailVo;
import com.sun.glass.ui.Size;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import static com.gzz.utils.OssUtils.GoodTypeId;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周佳成
 * @since 2023-11-21
 */
@RestController
@Slf4j
@RequestMapping("/api/food")
public class FoodController {

    @Autowired
    private FoodService foodService;
    @Autowired
    OssUploadService ossUploadService;
    @Autowired
    ImgsRespService imgsRespService;
    @PostMapping("/upload")
    public R<String> Scupload(String type,@RequestParam("file") MultipartFile multipartFile){
        String imgUrl = ossUploadService.uploadFile(multipartFile,type);
        if(imgUrl!="error"){
            return R.success(imgUrl);
        }
        return R.error(imgUrl);
    }
    @GetMapping("/list")
    public R<List<Food>> getAll(@RequestParam(name = "value") Integer value,@RequestParam(name = "curPage")Integer curPage,@RequestParam(name="pageSize") Integer pageSize)
    {
        return R.success(getListR(value, curPage, pageSize));
    }

    private List<Food> getListR(@RequestParam(name = "value") Integer value, @RequestParam(name = "curPage") Integer curPage, @RequestParam(name = "pageSize") Integer pageSize) {
        String searchValue = value.toString();
        List<Food> foodList = foodService.list();
        if(searchValue.equals("0"))
        {
            foodList.sort(new Comparator<Food>() {
                @Override
                public int compare(Food o1, Food o2) {
                    return o1.getDistant().doubleValue()-o2.getDistant().doubleValue()>0?1:-1;
                }
            });
        }
        else if(searchValue.equals("1"))
        {
            foodList.sort(new Comparator<Food>() {
                @Override
                public int compare(Food o1, Food o2) {
                    return o1.getVisits()-o2.getVisits()>0?1:-1;
                }
            });
        } else if(searchValue.equals("2"))
        {
                foodList.sort(new Comparator<Food>() {
                    @Override
                    public int compare(Food o1, Food o2) {
                       return o1.getScore()- o2.getScore()>0?1:-1;
                    }
                });
        }
        Integer num = foodList.size();
        Integer end = (curPage+1)*pageSize;
        if(num<(curPage+1)*pageSize){
            end = num;
        }
        foodList.subList(curPage*pageSize,end-1);
        for (Food food : foodList) {
            food.setTotNum(num);
        }
        return foodList;
    }

    @GetMapping("/find")
    public R<List<Food>> getResult(@RequestParam(name = "value") Integer value,@RequestParam(name = "curPage")Integer curPage,@RequestParam(name="pageSize") Integer pageSize,@RequestParam(name="searchValue") String findValue)
    {
        List<Food> list = getListR(value, curPage, pageSize);
        List<Food> foodList = new ArrayList<Food>();
        for (Food food : foodList) {
            if(StringIntersection.hasIntersection(findValue,food.toString()))
            {
                foodList.add(food);
            }
        }
            return R.success(foodList);
    }
    @GetMapping("/detail")
    public R<Food> getDetail(@RequestParam(name = "foodId") String foodId)
    {
        return R.success(foodService.getById(foodId));
    }

    @GetMapping("/findlist")
    public R<FoodList> getFindList(@RequestParam(name = "pageNum")Integer pageNum,@RequestParam(name = "pageSize")Integer pageSize,@RequestParam(name="searchFood") String searchFood,@RequestParam(name = "searchTips") String searchTips)
    {
        log.info("内容为"+searchTips+searchFood);
        QueryWrapper<Food> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(searchFood!="","food_Name",searchFood);
        queryWrapper.like(searchTips!="","food_Tips",searchTips);

        Page<Food> foodPage = new Page<>(pageNum,pageSize);
        List<Food> foodList = foodService.getFindList(foodPage,queryWrapper);
        log.info("内容为："+foodList.toString());
        List<FoodVo> foodVoList = foodService.getImage(foodList);
        FoodList foodList1 = new FoodList(foodVoList,foodService.list().size());
        return R.success(foodList1);
    }

    @GetMapping("/reset")
    public R<FoodList> getReset(@RequestParam(name = "pageNum")Integer pageNum,@RequestParam(name = "pageSize")Integer pageSize)
    {

        Page<Food> foodPage = new Page<>(pageNum,pageSize);
        foodService.page(foodPage);
        List<Food> foodList = foodPage.getRecords();
        List<FoodVo> foodVoList = foodService.getImage(foodList);
        FoodList  foodList1 = new FoodList(foodVoList,foodService.list().size());
        return R.success(foodList1);
    }

    @DeleteMapping("/delete")
    public R deleteFood(@RequestParam(name = "foodId") Integer foodId)
    {
        QueryWrapper<Food> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("food_Id",foodId);
        foodService.remove(queryWrapper);
        return R.success(true);
    }
    @PostMapping("/add")
    public  R<String> addDetail(HttpServletRequest httpServletRequest, @RequestBody FoodVo foodVo){
        //存入景区表
        Food food = new Food();

        BeanUtils.copyProperties(foodVo,food);

        foodService.save(food);

        //存入图片表
        List<String> imgs = foodVo.getImgs();
        log.info("图片路径为"+imgs.toString());
        Integer typeId = GoodTypeId;

        boolean r = imgsRespService.saveList(imgs,food.getFoodId(),typeId);

        if(!r){
            return R.error("数据异常");
        }

        return R.success("success");
    }
    @PutMapping("update")
    public R updateFood(@RequestBody FoodVo foodVo)
    {
        log.info("图片为"+foodVo.getFoodCoverImg());
        log.info(foodVo.getImgs().toString());
        foodService.removeByIdAndTypeId(foodVo.getFoodId(), GoodTypeId);
        foodService.removeById(foodVo.getFoodId());
        foodService.saveVo(foodVo);

        return R.success(true);
    }
}


